package com.example.login;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import com.example.R;
import com.example.mail.MailTabActivity;
import core.model.Game;


public class LoginActivity extends Activity {
    private static final String DEFAULT_LOGIN_DATA = "DEFAULT_LOGIN_DATA";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginlayout);

        final TextView party = (TextView) findViewById(R.id.loginParty);
        final TextView race = (TextView) findViewById(R.id.loginRace);
        final TextView password = (TextView) findViewById(R.id.loginPassword);

        final CheckBox rememberMeCheckBox = (CheckBox) findViewById(R.id.rememberMeCheckbox);

        if (isDefaultLoginDataStored()) {
            loadDefaultLoginData(party, race, password);
            rememberMeCheckBox.setChecked(true);
        }

        Button submit = (Button) findViewById(R.id.loginSubmit);
        submit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Game game = new Game();
                game.setParty(party.getText().toString());
                game.setRace(race.getText().toString());
                game.setPassword(password.getText().toString());

                if (rememberMeCheckBox.isChecked()) {
                    saveLoginData(game);
                }

                View loginView = findViewById(R.id.loginLayout);
                Intent i = new Intent(loginView.getContext(), MailTabActivity.class);
                i.putExtra(Game.GAME, game);
                loginView.getContext().startActivity(i);
            }
        });
    }

    private void loadDefaultLoginData(TextView party, TextView race, TextView password) {
        SharedPreferences settings = getSharedPreferences(DEFAULT_LOGIN_DATA, MODE_PRIVATE);
        party.setText(settings.getString(Game.PARTY, ""));
        race.setText(settings.getString(Game.RACE, ""));
        password.setText(settings.getString(Game.PASSWORD, ""));
    }

    private void saveLoginData(Game game) {
        SharedPreferences settings = getSharedPreferences(DEFAULT_LOGIN_DATA, MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();

        editor.putString(Game.PARTY, game.getParty());
        editor.putString(Game.RACE, game.getRace());
        editor.putString(Game.PASSWORD, game.getPassword());

        editor.commit();
    }


    public boolean isDefaultLoginDataStored() {
        SharedPreferences settings = getSharedPreferences(DEFAULT_LOGIN_DATA, MODE_PRIVATE);
        return settings.contains(Game.PARTY) && settings.contains(Game.RACE) && settings.contains(Game.PASSWORD) ;
    }
}
